package br.edu.ufcg.dsc.psoo.mymoney.view.action;

import java.util.List;

import org.mentawai.core.BaseAction;
import org.mentawai.filter.AuthenticationFree;

import br.edu.ufcg.dsc.psoo.mymoney.controller.AccountController;
import br.edu.ufcg.dsc.psoo.mymoney.controller.exceptions.AccountNotFoundException;
import br.edu.ufcg.dsc.psoo.mymoney.controller.exceptions.InvalidAccountException;
import br.edu.ufcg.dsc.psoo.mymoney.controller.exceptions.InvalidUserException;
import br.edu.ufcg.dsc.psoo.mymoney.controller.implementation.AccountControllerImpl;
import br.edu.ufcg.dsc.psoo.mymoney.model.bean.Account;
import br.edu.ufcg.dsc.psoo.mymoney.model.bean.User;
import br.edu.ufcg.dsc.psoo.mymoney.model.dao.AccountDAO;

public class AccountAction extends BaseAction implements AuthenticationFree {

	private AccountController accountController;

	public void setAccountDAO(AccountDAO accountDAO) {
		accountController = new AccountControllerImpl(accountDAO);
	}

	public boolean bypassAuthentication(String innerAction) {

		// if (innerAction != null && innerAction.equals("add")) return true;

		return true;// FIXME sempre autenticar
	}

	public void setLists() {

		Long userId = ((User) getUserSession()).getId();

		List<Account> list = accountController.listAll(userId);

		// System.out.println(list.toString());

		output.setValue("accountList", list);
	}

	public String delete(Long id) {

		if (!isPost()) {
			Long userId = ((User) getUserSession()).getId();

			try {
				accountController.delete(id, userId);
			} catch (InvalidAccountException e) {
				addMessage(e.getMessage());
				return ERROR;
			} catch (AccountNotFoundException e) {
				addMessage(e.getMessage());
				return ERROR;
			}

			addMessage("Conta removida com sucesso!");
			setLists();
			return SHOW;

		} else {
			return ERROR;
		}
	}

	public String add() {

		if (!isPost()) {

			setLists();

			return SHOW;

		} else {
			// recebe objeto montado do Input
			Account account = input.getObject(Account.class);
			User u = (User) getUserSession();
			account.setUser(u);

			try {
				accountController.create(account);
			} catch (InvalidAccountException e) {
				addMessage(e.getMessage());
				return ERROR;

			}

			addMessage("Conta cadastrada com sucesso!");

			setLists();

			return SHOW;
		}
	}

	public String update() {
		
		if (!isPost()) {
			
			Long id = Long.parseLong(input.getString("id"));
			
			// display user for update...

			Account acc = accountController.loadById(id);

			output.setValue("account", acc);

			return SHOW;

		} else {

			Account account = new Account();
			Long id = Long.parseLong(input.getString("id"));
			String name = input.getString("name");
			account.setId(id);
			account.setName(name);
			
			User loggedUser = (User) getUserSession();
			
			account.setUser(loggedUser);

			try {
				accountController.update(account);
			} catch (InvalidAccountException e) {
				addError("defaultError", e.getMessage());
				return SHOW;
			}

			addMessage("Informacoes alteradas com sucesso!");

			return UPDATED;
		}
	}
}